<template>
  <div class="app-container">
    <!-- 字典列表 -->
    <el-row :gutter="10">
      <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12" style="margin-bottom: 10px">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>任务列表</span>
          </div>
          <!--工具栏-->
          <div class="head-container">
            <!-- 搜索 -->
            <el-input v-model="query.blurry" clearable size="small" placeholder="输入编码或者名称搜索" style="width: 200px;"
                      class="filter-item" @keyup.enter.native="toQuery"/>
            <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="toQuery">搜索
            </el-button>
            <el-button class="filter-item" v-if="checkPermission(['appMutex:add'])" size="mini" type="primary" icon="el-icon-plus" @click="addMutexTask">新增互斥
            </el-button>
          </div>
          <!--表格渲染-->
          <el-table v-loading="loading" :data="data" highlight-current-row style="width: 100%;"
                    @selection-change="selectionLineChangeHandle" border stripe
                    :header-cell-style="{ background: 'rgba(52, 125, 225, 0.1)', color: '#404248' }">
            <el-table-column type="selection"/>
            <el-table-column :show-overflow-tooltip="true" prop="taskCode" label="任务编码"/>
            <el-table-column :show-overflow-tooltip="true" prop="taskName" label="任务名称"/>
            <el-table-column :show-overflow-tooltip="true" prop="resNum" label="资源数"/>
          </el-table>
          <!--分页组件-->
          <el-pagination :total="total" :current-page="page + 1" style="margin-top: 8px;"
                         layout="prev, pager, next, sizes,total"
                         @size-change="sizeChange"
                         @current-change="pageChange"
                         :pager-count="3" prev-text=" " next-text=" "
          />
        </el-card>
      </el-col>
      <!-- 字典详情列表 -->
      <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>互斥任务列表</span>
          </div>
          <mutex-detail ref="appMutexDetail"/>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import crud from '@/mixins/crud'
import mutexDetail from './mutexDetail'
import crudDict from '@/api/system/dict'
import mutexApi from '@/api/mutexPool/mutex'
import checkPermission from '@/utils/permission' // 权限判断函数

export default {
  name: 'appMutex',
  components: {mutexDetail},
  mixins: [crud],
  data() {
    return {
      title: '互斥池',
      crudMethod: {...crudDict},
      queryTypeOptions: [
        {key: 'name', display_name: '字典名称'},
        {key: 'remark', display_name: '描述'}
      ],
      form: {id: null, name: null, remark: null},
      totalElements: 0,
      rules: {
        name: [
          {required: true, message: '请输入名称', trigger: 'blur'}
        ]
      },
      dataonLineListSelections: [],
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    checkPermission,
    // 获取数据前设置好接口地址
    beforeInit() {
      this.url = 'api/periodTaskDef/selectPage';
      this.$refs.appMutexDetail.init()
      return true
    },
    selectionLineChangeHandle(val) {
      this.dataonLineListSelections = val
    },
    addMutexTask() {
      if (this.dataonLineListSelections.length > 0) {
        let taskCodeList = [];
        for (var i = 0; i < this.dataonLineListSelections.length; i++) {
          taskCodeList.push(this.dataonLineListSelections[i].taskCode);
        }
        this.$confirm(`确认将选中的${taskCodeList.length}条任务添加到互斥池吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let param = {taskCodes: taskCodeList}
          mutexApi.addAppMutexPool(param).then(() => {
            this.$notify({message: '添加成功', type: 'success'});
            this.init();
            this.$refs.appMutexDetail.init();
          }).catch((res) => {
            this.$notify.error({title: '添加失败', message: res.response.data});
          })
        }).catch(() => {
        })
      } else {
        this.$notify({title: '警告', message: '请选择至少一条任务', type: 'warning'});
      }
    }
  }
}
</script>

<style scoped>
</style>

